<script setup>
import * as areaApi from '@/api/basic/area.js';
import * as api from '@/api/public'
import { ElMessage, ElMessageBox } from 'element-plus';
import { reactive, ref } from 'vue';
import EditDialog from './components/EditDialog.vue';
    // 列表数据
    const list = reactive({data:[]})

    // form表单类============
    let form = ref({})
    // ==============

    let dialogVisible = ref(false);

    // 初始化
    const init = () =>{
      areaApi.getTree().then(res =>{
            console.log(res)
            list.data = res.data
        })
    }

    init()

    // 弹窗
    const dialog = (v,s) => {
      console.log(v)
        let copyParentCode = v ? v.parentCode : '0';
        if(v) v.parentCode = v.code;
        form.value = v ? v : {
          id: '',
          code: '',
          name: '',
          level: 0,
          parentCode: '0',
          firstLetter: ''
        };
        form.value.treeData = list.data;
        form.value.status = s;
        if(v) form.value.copyParentCode = copyParentCode;
        dialogVisible.value = true;
    }

    // 删除方法
    const del = (id) => {
        ElMessageBox.confirm('请确认删除...', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
          api.remove({id},'/area').then((res) => {
                ElMessage({
                    message: res.msg,
                    type: 'success'
                })
                init()
            })
        })
    }
</script>
<template>
    <div class="container">
        <div class="head">
            <!-- 查询功能组件 -->
            <div class="bottom">
                <!-- 添加按钮 -->
                <el-button type="primary" @click="dialog(null,0)">添加</el-button>
            </div>
        </div>
        <div class="middle">
            <!-- 表格 -->
            <el-table
                :data="list.data"
                row-key="id"
                style="width: 100%"
                :tree-props="{ children: 'children'}"
            >
                <el-table-column prop="name" label="地区名" />
                <el-table-column prop="level" label="地区级别" />
                <el-table-column prop="firstLetter" label="首字母" />
                <el-table-column fixed="right" label="操作" min-width="100">
                    <template #default="scope">
                        <el-button type="primary" size="small" @click="dialog(scope.row,0)" v-if="scope.row.type !== '2'">添加</el-button>
                        <el-button type="warning" size="small" @click="dialog(scope.row,1)">修改</el-button>
                        <el-button type="danger" size="small" @click="del(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 编辑弹窗 -->
        <edit-dialog @success="init" :data="form" v-model="dialogVisible" />
    </div>
</template>
<style lang="scss" scoped>
    .container{
        height: 100%;
        display: flex;
        flex-direction: column;
        .head{
            margin-bottom: 6px;
        }
        .middle{
            margin-bottom: 12px;
        }
    }
</style>
